package com.example.springbootquartz.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;
import lombok.Data;


@Data
public class ResponseVo<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 成功标志
	 */
	private boolean success = true;

	/**
	 * 返回处理消息
	 */
	private String message = "";

	/**
	 * 返回代码
	 */
	private Integer code = 0;
	
	/**
	 * 返回数据对象 data
	 */
	private T result;
	
	/**
	 * 时间戳
	 */
	private long timestamp = System.currentTimeMillis();

	public ResponseVo() {
	}

    /**
     * 兼容VUE3版token失效不跳转登录页面
     * @param code
     * @param message
     */
	public ResponseVo(Integer code, String message) {
		this.code = code;
		this.message = message;
	}
	
	public ResponseVo<T> success(String message) {
		this.message = message;
		this.code = Constant.SC_OK_200;
		this.success = true;
		return this;
	}

	public static<T> ResponseVo<T> ok() {
		ResponseVo<T> r = new ResponseVo<T>();
		r.setSuccess(true);
		r.setCode(Constant.SC_OK_200);
		return r;
	}

	public static<T> ResponseVo<T> ok(String msg) {
		ResponseVo<T> r = new ResponseVo<T>();
		r.setSuccess(true);
		r.setCode(Constant.SC_OK_200);
		//Result OK(String msg)方法会造成兼容性问题 issues/I4IP3D
		r.setResult((T) msg);
		r.setMessage(msg);
		return r;
	}

	public static<T> ResponseVo<T> status(boolean isSuccess) {
		return isSuccess? ok() : error("数据处理失败");
	}

	public static<T> ResponseVo<T> ok(T data) {
		ResponseVo<T> r = new ResponseVo<T>();
		r.setSuccess(true);
		r.setCode(Constant.SC_OK_200);
		r.setResult(data);
		return r;
	}

	public static<T> ResponseVo<T> OK() {
		ResponseVo<T> r = new ResponseVo<T>();
		r.setSuccess(true);
		r.setCode(Constant.SC_OK_200);
		return r;
	}

	/**
	 * 此方法是为了兼容升级所创建
	 *
	 * @param msg
	 * @param <T>
	 * @return
	 */
	public static<T> ResponseVo<T> OK(String msg) {
		ResponseVo<T> r = new ResponseVo<T>();
		r.setSuccess(true);
		r.setCode(Constant.SC_OK_200);
		r.setMessage(msg);
		//Result OK(String msg)方法会造成兼容性问题 issues/I4IP3D
		r.setResult((T) msg);
		return r;
	}

	public static<T> ResponseVo<T> OK(T data) {
		ResponseVo<T> r = new ResponseVo<T>();
		r.setSuccess(true);
		r.setCode(Constant.SC_OK_200);
		r.setResult(data);
		return r;
	}

	public static<T> ResponseVo<T> OK(String msg, T data) {
		ResponseVo<T> r = new ResponseVo<T>();
		r.setSuccess(true);
		r.setCode(Constant.SC_OK_200);
		r.setMessage(msg);
		r.setResult(data);
		return r;
	}

	public static<T> ResponseVo<T> error(String msg, T data) {
		ResponseVo<T> r = new ResponseVo<T>();
		r.setSuccess(false);
		r.setCode(Constant.SERVER_ERROR_500);
		r.setMessage(msg);
		r.setResult(data);
		return r;
	}

	public static<T> ResponseVo<T> error(String msg) {
		return error(Constant.SERVER_ERROR_500, msg);
	}
	
	public static<T> ResponseVo<T> error(int code, String msg) {
		ResponseVo<T> r = new ResponseVo<T>();
		r.setCode(code);
		r.setMessage(msg);
		r.setSuccess(false);
		return r;
	}

	public ResponseVo<T> error500(String message) {
		this.message = message;
		this.code = Constant.SERVER_ERROR_500;
		this.success = false;
		return this;
	}


	@JsonIgnore
	private String onlTable;

}